package com.gzlz.budejie.view;

import java.util.ArrayList;
import java.util.List;

import name.fuhan.shipin.ShiPinView;
import name.fuhan.tupian_fh.MyImageView;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.gzlz.budejie.PingLunActivity;
import com.gzlz.budejie.R;
import com.gzlz.budejie.activity.GukeActivity;
import com.gzlz.budejie.bean.JItemData;
import com.gzlz.budejie.bean.JItemData.TieZi;
import com.gzlz.budejie.bean.JItemData.TieZi.ZuiRe;
import com.gzlz.budejie.utils.BMUtils;
import com.gzlz.budejie.utils.LineInfo;
import com.gzlz.budejie.utils.SP;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.viewpagerindicator.TabPageIndicator;

/**
 * 精华页面的中间部分
 * @author FuHan
 *
 */
public class LieBiaoView2 extends LinearLayout {
	public LieBiaoView2(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	
	private TabPageIndicator indicator;
	private ViewPager pager;
	private String[] title = {"全部","视频","图片","段子","声音"};
	private List<ListView> data = new ArrayList<ListView>(title.length);
	
	/**
	 * 保存下拉组件的集合
	 */
	ArrayList<SwipeRefreshLayout> srls = new ArrayList<SwipeRefreshLayout>(title.length);
	
	/**
	 * 包裹下拉控件和listview的视图 {@link R.layout.liebiao_item_xiala_list}
	 */
	ArrayList<View> xialaData = new ArrayList<View>(title.length) ;
	
	/**
	 * 显示"加载中"字样的view,未使用
	 */
	public View shimmer_tv;
	
//	public Bitmap 让精彩填满生活;
//	private ShimmerTextView shimmer_tv;

	
	
	ArrayList<String> lastMaxtime = new ArrayList<String>();
	{
		for (int i = 0; i < title.length; i++) {
			lastMaxtime.add("0");
		}
	}

	private String 获取链接(int position) {
		String url图片 = "http://api.budejie.com/api/api_open.php?c=data&a=list&type=10&maxtime=";
		String url全部 = "http://api.budejie.com/api/api_open.php?c=data&a=list&type=1&maxtime=";
		String url视频 = "http://api.budejie.com/api/api_open.php?c=data&a=list&type=41&maxtime=";
		String url段子 = "http://api.budejie.com/api/api_open.php?c=data&a=list&type=29&maxtime=";
		String url排行 = "http://api.budejie.com/api/api_open.php?c=post&a=list&maxtime=";
		//TODO 需保存每屏的ItemData.info.maxtime,在数据获取后
		String url声音 = "http://api.budejie.com/api/api_open.php?c=voice&a=list&per=20&maxtime=";
		String url = "";
		switch (position) {
		case 0:
			url = url全部;
			break;
		case 1:
			url = url视频;
			break;
		case 2:
			url = url图片;
			break;
		case 3:
			url = url段子;
			break;
		case 4:
			url = url声音;
			break;
		default:
			break;
		}
		url+= lastMaxtime.get(position);
		return url;
	}

	
	
	
	
	
	/**
	 * 当视图展示到界面
	 */
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		indicator = (TabPageIndicator) findViewById(R.id.indicator);
		pager = (ViewPager) findViewById(R.id.pager);
//		让精彩填满生活 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_post_copyright);
		pager.setAdapter(new MyPagerAdapter());
		indicator.setViewPager(pager);

		
		for (int i = 0; i < title.length; i++) {
			View liebiao_item_xiala_list = View.inflate(getContext(), R.layout.liebiao_item_xiala_list, null);
			SwipeRefreshLayout swipe_container = (SwipeRefreshLayout)liebiao_item_xiala_list.findViewById(R.id.swipe_container);
			ListView listView = (ListView) liebiao_item_xiala_list.findViewById(R.id.listview);
			
			listView.setCacheColorHint(0);
			swipe_container.setOnRefreshListener(new MyOnRefreshListener(i));
			srls.add(swipe_container);
			data.add(listView);
			xialaData.add(liebiao_item_xiala_list);
		}
//		initData(data.get(0), 0);
	}
//	ArrayList<ShimmerTextView> shimmer_tvs = new ArrayList<ShimmerTextView>();
	
	/**
	 * 下拉刷新用的
	 * @author FuHan
	 *
	 */
public class MyOnRefreshListener implements OnRefreshListener {
		int index ;
		public MyOnRefreshListener(int index){
			this.index = index;
		}
		@Override
		public void onRefresh() {
			Toast.makeText(getContext(), "刷新"+index, 0).show();
			//下拉刷新 
			LinearLayout parent = (LinearLayout) LieBiaoView2.this.getParent().getParent();
			HeadView headView = (HeadView) parent.findViewById(R.id.head_view);
			headView.startHeadImage2RotateAnimation();
			
			int currentItem = pager.getCurrentItem();//当前展示的page
			
			lastMaxtime.set(currentItem, "0");
			
			initData(data.get(currentItem),currentItem);
			
			
			
			/*
			//刷新完成时
			headView.stopHeadImage2RotateAnimation();
			srls.get(index).setRefreshing(false);*/
			
		}
	}
	
		
	/**
	 * viewPage的适配器
	 * @author FuHan
	 *
	 */
	public class MyPagerAdapter extends PagerAdapter{

		@Override
		public CharSequence getPageTitle(int position) {
			return title[position];
		}
		
		
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			//获取title[position]页的数据
			
			ListView listView = data.get(position);
			
			//加载数据
			initData(listView, position);
			
			View view = xialaData.get(position);
			container.addView(view);
			return view;
		}
		
		
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
//			super.destroyItem(container, position, object);
			container.removeView((View) object);
		}
		
		@Override
		public int getCount() {
			return title.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0==arg1;
		}
		
	}
	Gson gson = new Gson();
	/**
	 * 连接网络获取数据并保存到listView
	 * @param listView 保存到该listView中
	 * @param position 哪一页
	 */
	public void initData(final ListView listView, final int position) {
		// 连接网络获取数据并保存到listView
//		System.gc();
		
		
		
		String url = 获取链接(position);
		
		String bendishuju = SP.getString(url);
		if (bendishuju!=null) {
//			Log.e("TGAB", "<url:"+url+">本地数据:"+bendishuju);
			JItemData fromJson =gson.fromJson(bendishuju, JItemData.class);
//			Log.e("TGAC", "position"+position+",url"+url+"fromJson.info.maxtime"+fromJson.info.maxtime);
			if (!"0".equals(url.substring(url.length()-1))) {
				lastMaxtime.set(position, fromJson.info.maxtime);//保存本次的记录
				initListViewData(listView, fromJson);
				return;
			}else {
				initListViewData(listView, fromJson);
			}
		}
		//TODO 展示读取界面
//		shimmer_tvs.get(position).setVisibility(View.VISIBLE);
//		listView.addHeaderView(shimmer_tv);
//		shimmer_tv.setVisibility(View.VISIBLE);
//		FrameLayout parent = (FrameLayout) LieBiaoView.this.getParent();
//		View shimmer_tv = parent.findViewById(R.id.shimmer_tv);
		if(shimmer_tv!=null)
			shimmer_tv.setVisibility(View.VISIBLE);
		
				
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, url, new RequestCallBack<String>() {

	

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				//获取失败时
				Log.e("TGAB", "读取网络数据失败(LieBiaoView):"+arg1);
				Toast.makeText(getContext(), "您的网络不通!", 0).show();
				
				LinearLayout parent2 = (LinearLayout) LieBiaoView2.this.getParent().getParent();
				HeadView headView = (HeadView) parent2.findViewById(R.id.head_view);
				headView.stopHeadImage2RotateAnimation();
				srls.get(position).setRefreshing(false);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				Log.e("TGAB", getRequestUrl());
				
				JItemData itemData;
				try {
					itemData = gson.fromJson(arg0.result, JItemData.class);
					SP.putString(getRequestUrl(), arg0.result);
//				Log.e("TGAB", itemData.toString());
					lastMaxtime.set(position, itemData.info.maxtime);//保存本次的记录
					
					
					initListViewData(listView, itemData);
					
				} catch (JsonSyntaxException e) {
					e.printStackTrace();
					Toast.makeText(getContext(), "json解析错误", 0).show();
				}
				
				
				
				
				
				//刷新完成时
				
//				shimmer_tvs.get(position).setVisibility(View.GONE);
//				listView.removeHeaderView(shimmer_tv);
//				shimmer_tv.setVisibility(View.GONE);
//				FrameLayout parent = (FrameLayout) LieBiaoView.this.getParent();
//				View shimmer_tv = parent.findViewById(R.id.shimmer_tv);
				if(shimmer_tv!=null)
					shimmer_tv.setVisibility(View.GONE);
				
				
				LinearLayout parent2 = (LinearLayout) LieBiaoView2.this.getParent().getParent();
				HeadView headView = (HeadView) parent2.findViewById(R.id.head_view);
				headView.stopHeadImage2RotateAnimation();
				srls.get(position).setRefreshing(false);
				
			}
		});
		
	}
	
	
	/**
	 * 为listView添加适配器
	 * @param listView 
	 * @param itemData 数据
	 */
	public void initListViewData(ListView listView,JItemData itemData){
		listView.setAdapter(new MyListAdapter(listView,itemData));
		
		
//		((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
	}
	
	
	public class MyListAdapter extends BaseAdapter{
		JItemData itemData ;
		ListView listView;
		public MyListAdapter(ListView listView , JItemData itemData) {
			this.itemData = itemData;
			this.listView = listView;
		}

		class viewHolder{
			String type ;
//			BitmapUtils bitmapUtils = new BitmapUtils(getContext());
			ImageView liebiao_item_icon;//头像
			TextView liebiao_item_name;//名字
			TextView liebiao_item_time;//时间
			TextView liebiao_item_text;//文字内容
//			LinearLayout liebiao_item_view;//图片内容
			ImageView liebiao_item_ding_img;//点赞
			TextView liebiao_item_ding_tv;//点赞数
			ImageView liebiao_item_cai_img;//踩
			TextView liebiao_item_cai_tv;//踩数
			ImageView liebiao_item_zhuanfa_img;//转发
			TextView liebiao_item_zhuanfa_tv;//转发数
			ImageView liebiao_item_pinglun_img;//评论
			TextView liebiao_item_pinglun_tv;//评论数
			LinearLayout liebiao_item_zuire_ll;//最热评论区域
			TextView liebiao_item_zuire_text;//最热评论内容
			
			
			ShiPinView shiPinView;//视频用的
			MyImageView tupianView;//图片用的
			VoiceView shengyinView;//声音
			
			
		}

		private userOnClickListener userOnClickListener = new userOnClickListener();

		boolean 正在发送中;
		private OnClickListener on点赞Listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				final TieZi tiezi = (TieZi) v.getTag();
				final LinearLayout dingParent = (LinearLayout) v.getParent();
				final ImageView dingIV = (ImageView) dingParent.getChildAt(0);
				final TextView dingTV = (TextView) dingParent.getChildAt(1);
				LinearLayout parent = (LinearLayout) dingParent.getParent();
				LinearLayout caiParent = (LinearLayout) parent.getChildAt(2);
				ImageView caiIV = (ImageView) caiParent.getChildAt(0);
				
				if (!正在发送中&&!dingIV.isSelected()&&!caiIV.isSelected()) {//没点赞,也没踩,
					正在发送中 = true;
					new HttpUtils().send(HttpMethod.GET, "http://api.budejie.com/api/api_open.php?c=post&a=love&id="+tiezi.id, new RequestCallBack<String>() {

						@Override
						public void onFailure(HttpException arg0, String arg1) {
							正在发送中 = false;
							Toast.makeText(getContext(), "网络请求失败,无法点赞!", 0).show();
						}

						@Override
						public void onSuccess(final ResponseInfo<String> arg0) {
							正在发送中 = false;
							if (arg0.result.equals("0")) {
								Toast.makeText(getContext(), "请求失败!", 0).show();
								return;
							}
							SP.putString(SP.顶踩帖子+tiezi.id, SP.顶);
							
							dingIV.setSelected(true);
							dingTV.setTextColor(Color.RED);
							dingTV.setText("+1");
							AlphaAnimation animation = new AlphaAnimation(0, 1);
							animation.setDuration(200);
							AlphaAnimation animation3 = new AlphaAnimation(1, 0);
							animation3.setStartOffset(200);
							animation3.setDuration(300);
							ScaleAnimation animation2 = new ScaleAnimation(0.2f, 1, 0.2f, 1);
							animation2.setDuration(400);
							AnimationSet animationSet = new AnimationSet(false);
							animationSet.addAnimation(animation);
							animationSet.addAnimation(animation3);
							animationSet.addAnimation(animation2);
							animationSet.setAnimationListener(new AnimationListener() {
								@Override
								public void onAnimationStart(Animation animation) {}
								@Override
								public void onAnimationRepeat(Animation animation) {}
								@Override
								public void onAnimationEnd(Animation animation) {
									dingTV.setText(arg0.result);
								}
							});
							dingTV.startAnimation(animationSet);
						}
					});
				}
			}
		};
		private OnClickListener on踩Listener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final TieZi tiezi = (TieZi) v.getTag();
				final LinearLayout caiParent = (LinearLayout) v.getParent();
				final ImageView caiIV = (ImageView) caiParent.getChildAt(0);
				final TextView caiTV = (TextView) caiParent.getChildAt(1);
				LinearLayout parent = (LinearLayout) caiParent.getParent();
				LinearLayout dingParent = (LinearLayout) parent.getChildAt(0);
				ImageView dingIV = (ImageView) dingParent.getChildAt(0);
				
				if (!正在发送中&&!caiIV.isSelected()&&!dingIV.isSelected()) {//没点赞,也没踩,
					正在发送中 = true;
					new HttpUtils().send(HttpMethod.GET, "http://api.budejie.com/api/api_open.php?c=post&a=cai&id="+tiezi.id, new RequestCallBack<String>() {


						@Override
						public void onFailure(HttpException arg0, String arg1) {
							正在发送中 = false;
							Toast.makeText(getContext(), "网络请求失败,无法点赞!", 0).show();
						}

						@Override
						public void onSuccess(final ResponseInfo<String> arg0) {
							正在发送中 = false;
							if (arg0.result.equals("0")) {
								Toast.makeText(getContext(), "请求失败!", 0).show();
								return;
							}
							SP.putString(SP.顶踩帖子+tiezi.id, SP.踩);
							caiIV.setSelected(true);
							caiTV.setTextColor(Color.RED);
							caiTV.setText("+1");
							AlphaAnimation animation = new AlphaAnimation(0, 1);
							animation.setDuration(200);
							AlphaAnimation animation3 = new AlphaAnimation(1, 0);
							animation3.setStartOffset(200);
							animation3.setDuration(300);
							ScaleAnimation animation2 = new ScaleAnimation(0.2f, 1, 0.2f, 1);
							animation2.setDuration(400);
							AnimationSet animationSet = new AnimationSet(false);
							animationSet.addAnimation(animation);
							animationSet.addAnimation(animation3);
							animationSet.addAnimation(animation2);
							animationSet.setAnimationListener(new AnimationListener() {
								@Override
								public void onAnimationStart(Animation animation) {}
								@Override
								public void onAnimationRepeat(Animation animation) {}
								@Override
								public void onAnimationEnd(Animation animation) {
									caiTV.setText(arg0.result);
								}
							});
							caiTV.startAnimation(animationSet);
						}
					});
				}
			}
		};
		
		
		private OnClickListener on转发Listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO 待完成
				TieZi tiezi = (TieZi) v.getTag();
				
			}
		};
		
		private OnClickListener on评论Listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				TieZi tiezi = (TieZi) v.getTag();
				Intent intent = new Intent(getContext(),PingLunActivity.class);
				intent.putExtra("tiezi", tiezi);
				getContext().startActivity(intent);
			}
		};
		
		
		
		TextView textView = new TextView(getContext());
		{
			textView.setText("如果你看到了这一条...那么你的网络可能有点问题╮(╯▽╰)╭");
		}
		
		BitmapDisplayConfig  bigPicDisplayConfig = new BitmapDisplayConfig();
		
		//加载数据到listView
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			
			
			//如果是最后一个Item
			if (position==getCount()-1) {
				return textView;
			}
			
			
			
			
			TieZi tieZi = itemData.list.get(position);
			if(tieZi.type.equals("10")&&TextUtils.isEmpty(tieZi.image1)){
				throw new RuntimeException("出现NULL  position="+position+",帖子:"+tieZi);
			}
			
			//如果被复用的是提示信息,那么不进行复用
			if (convertView == textView) {
				convertView = null;
			}
			
			
			//将convertView送去复用,根据type取得待复用列表里的view
			convertView = converView池.getConvertView(convertView,tieZi.type);
			
			viewHolder holder = (viewHolder) convertView.getTag();
			
			
			
			holder.liebiao_item_ding_tv.setText(tieZi.love);//顶
			holder.liebiao_item_cai_tv.setText(tieZi.hate);//踩
			holder.liebiao_item_zhuanfa_tv.setText(tieZi.repost);//转发
			holder.liebiao_item_pinglun_tv.setText(tieZi.comment);//评论数
			holder.liebiao_item_name.setText("id:"+tieZi.screen_name);//名字
			holder.liebiao_item_text.setText(tieZi.text);//正文内容
			holder.liebiao_item_time.setText(tieZi.created_at);//发布时间
			
			//头像名字点击事件
			holder.liebiao_item_name.setTag(tieZi.user_id);
			holder.liebiao_item_icon.setTag(tieZi.user_id);
			
			String 顶踩帖子 = SP.getString(SP.顶踩帖子+tieZi.id);
			holder.liebiao_item_ding_img.setSelected(SP.顶.equals(顶踩帖子));
			holder.liebiao_item_cai_img.setSelected(SP.踩.equals(顶踩帖子));
			
			//顶
			holder.liebiao_item_ding_img.setTag(tieZi);
			holder.liebiao_item_ding_tv.setTag(tieZi);
			
			//踩
			holder.liebiao_item_cai_img.setTag(tieZi);
			holder.liebiao_item_cai_tv.setTag(tieZi);

			//转发,分享
			holder.liebiao_item_zhuanfa_img.setTag(tieZi);
			holder.liebiao_item_zhuanfa_tv.setTag(tieZi);
			
			//评论
			holder.liebiao_item_pinglun_img.setTag(tieZi);
			holder.liebiao_item_pinglun_tv.setTag(tieZi);
			
			
			
			//读取最热评论
			ArrayList<ZuiRe> top_cmt = tieZi.top_cmt;
			if (top_cmt==null||top_cmt.size()==0||!(top_cmt.get(0) instanceof ZuiRe)) {//如果没有最热评论,隐藏
				holder.liebiao_item_zuire_ll.setVisibility(View.GONE);
			}else {
				holder.liebiao_item_zuire_ll.setVisibility(View.VISIBLE);
				ZuiRe zuiRe = top_cmt.get(0);
				holder.liebiao_item_zuire_text.setText(zuiRe.user.username+"："+zuiRe.content);
			}
			
			
			
			
			
//			holder.liebiao_view.setImageBitmap(让精彩填满生活);
			if(!TextUtils.isEmpty(tieZi.profile_image)) {//设置头像
//				BitmapUtils bitmapUtils = holder.bitmapUtils;
				bigPicDisplayConfig.setBitmapConfig(Bitmap.Config.RGB_565);
				BMUtils.display(holder.liebiao_item_icon, tieZi.profile_image,bigPicDisplayConfig);
			}

			
			
			
//			holder.liebiao_item_view.removeAllViews();//测试用,已无用
			
			switch (Integer.parseInt(tieZi.type)) {
			case 10://图片
				String is_gif = tieZi.is_gif;
				String address ;
				String address2;
				if("1".equals(is_gif)){
					address = tieZi.gifFistFrame;
				}else {
					address = tieZi.image0;
				}
				address2 = tieZi.image1;
				if (address==null) {
					throw new RuntimeException("地址出现null,"+tieZi);
				}
				holder.tupianView.setAddress(address, address2, "1".equals(is_gif),tieZi.width,tieZi.height);
				
				/*View childAt = holder.liebiao_item_view.getChildAt(0);
				String is_gif = tieZi.is_gif;
				String address ;
				String address2;
				if("1".equals(is_gif)){
					address = tieZi.gifFistFrame;
				}else {
					address = tieZi.image0;
				}
				address2 = tieZi.image1;
				if (childAt!=null &&childAt instanceof MyImageView) {
					((MyImageView)childAt).position = position;
					((MyImageView)childAt).setAddress(address,address2,is_gif);
				}else if(childAt == null||!(childAt instanceof MyImageView)){
					holder.liebiao_item_view.removeAllViews();
					holder.liebiao_item_view.addView(chi.getImageView(position).setAddress(address,address2,is_gif));
				}
				break;*/
			case 29://段子
//				holder.liebiao_item_view.removeAllViews();
				//啥也不干
				break;
			case 41://视频
				String videoAddress = tieZi.videouri;
				String imgAdress = tieZi.cdn_img;
				int height = Integer.parseInt(tieZi.height);
				int width = Integer.parseInt(tieZi.width);
				
				holder.shiPinView.setAddress(imgAdress, videoAddress, listView, width, height,position);
				
//				View childAt1 = holder.liebiao_item_view.getChildAt(0);
//				if (childAt1!=null &&childAt1 instanceof ShiPinView) {
//					((ShiPinView)childAt1).setAddress(imgAdress, videoAddress,listView,height,width);
//				}else if(childAt1 == null||!(childAt1 instanceof ShiPinView)){
//					holder.liebiao_item_view.removeAllViews();
//					holder.liebiao_item_view.addView(chi.getShiPinView(position).setAddress(imgAdress, videoAddress,listView,height,width));
//				}
				break;
			case 31://声音
				holder.shengyinView.setAddress(tieZi.voiceuri, tieZi.image0);
				break;
			default:
				throw new RuntimeException(LineInfo.getLineInfo()+"<LiebiaoView.getView()#691行>出现异常type"+tieZi.type);
			}
			
			
			//TODO 加载更多
			if (position==getCount()-2) {
				加载更多(itemData);
			}
			
			
			return convertView;
		}
		
		ConverViewChi converView池 = new ConverViewChi();
		class ConverViewChi {
			
			ArrayList<View> tupianList = new ArrayList<View>();
			ArrayList<View> shipinList = new ArrayList<View>();
			ArrayList<View> duanziList = new ArrayList<View>();
			ArrayList<View> shengyinList = new ArrayList<View>();
			int tuPian = 0;
			/**
			 * 
			 * @param convertView2 被复用的那个视图
			 * @param type 需要的类型
			 * @return
			 */
		public  View getConvertView(View convertView2, String type) {
			
			Log.i("TGAL", "图片"+tupianList.size()+",视频"+shipinList.size()+",段子"+duanziList.size()+",声音"+shengyinList.size());
			
			if (convertView2!=null) {
				viewHolder holder = (viewHolder) convertView2.getTag();
				switch (Integer.parseInt(holder.type)) {
				//TODO 当item被重用,关掉该Item中的下载任务
				case 10://  41视频,10图片，29段子,20声音，1（图片、段子、声音、视频混排）
//					holder.tupianView.初始化();
					Log.i("TGAL", "一张图片被复用");
					tupianList.add(convertView2);
					break;
				case 41:
					holder.shiPinView.重置();
					shipinList.add(convertView2);
					break;
				case 29:
					duanziList.add(convertView2);
					break;
				case 31:
					shengyinList.add(convertView2);
					break;
				default:
					break;
				}
			}
			
			View view;
			viewHolder holder;
			switch (Integer.parseInt(type)) {
				case 10://  10图片，41视频,29段子,31声音，1（图片、段子、声音、视频混排）
					if (tupianList.size()==0) {//待重用列表里有就直接返回,没有就读XML
						Log.d("TGAL", "新建了一个图片"+ ++tuPian);
						view =  View.inflate(getContext(), R.layout.liebiao_item_tupian, null);
						holder = new viewHolder();
						holder.type = type;
						holder.tupianView = (MyImageView) view.findViewById(R.id.liebiao_item_imageview);
					}else {
						return tupianList.remove(0);
					}
					break;
				case 41:
					if (shipinList.size()==0) {//待重用列表里有就直接返回,没有就读XML
						view =  View.inflate(getContext(), R.layout.liebiao_item_shipin, null);
						holder = new viewHolder();
						holder.type = type;
						holder.shiPinView = (ShiPinView) view.findViewById(R.id.liebiao_item_shipinview);
					}else {
						return shipinList.remove(0);
					}
					break;
				case 29:
					if (duanziList.size()==0) {//待重用列表里有就直接返回,没有就读XML
						view =  View.inflate(getContext(), R.layout.liebiao_item, null);
						holder = new viewHolder();
						holder.type = type;
					}else {
						return duanziList.remove(0);
					}
					break;
				case 31:
					if (shengyinList.size()==0) {//待重用列表里有就直接返回,没有就读XML
						view =  View.inflate(getContext(), R.layout.liebiao_item_shengyin, null);
						holder = new viewHolder();
						holder.type = type;
						holder.shengyinView = (VoiceView) view.findViewById(R.id.liebiao_item_shengyinview);
					}else {
						return shengyinList.remove(0);
					}
					break;
				default:
					Log.e("TGAERR", LineInfo.getLineInfo()+"<LieBiaoView:790行>出现了一个新的未处理类型:"+type);
					throw new RuntimeException(LineInfo.getLineInfo()+"一个新的类型:"+type);
			}
			
			//所有类型通用的内容
			holder.liebiao_item_icon = (ImageView) view.findViewById(R.id.liebiao_item_icon);//头像
			holder.liebiao_item_name = (TextView) view.findViewById(R.id.liebiao_item_name);//名字
			holder.liebiao_item_time = (TextView) view.findViewById(R.id.liebiao_item_time);//时间
			holder.liebiao_item_text = (TextView) view.findViewById(R.id.liebiao_item_text);//文字内容
//			holder.liebiao_item_view = (LinearLayout) view.findViewById(R.id.liebiao_item_view);//多媒体内容
			holder.liebiao_item_ding_img = (ImageView) view.findViewById(R.id.liebiao_item_ding_img);//点赞
			holder.liebiao_item_ding_tv = (TextView) view.findViewById(R.id.liebiao_item_ding_tv);//点赞数
			holder.liebiao_item_cai_img = (ImageView) view.findViewById(R.id.liebiao_item_cai_img);//踩
			holder.liebiao_item_cai_tv = (TextView) view.findViewById(R.id.liebiao_item_cai_tv);//踩数
			holder.liebiao_item_zhuanfa_img = (ImageView) view.findViewById(R.id.liebiao_item_zhuanfa_img);//转发
			holder.liebiao_item_zhuanfa_tv = (TextView) view.findViewById(R.id.liebiao_item_zhuanfa_tv);//转发数
			holder.liebiao_item_pinglun_img = (ImageView) view.findViewById(R.id.liebiao_item_pinglun_img);//评论
			holder.liebiao_item_pinglun_tv = (TextView) view.findViewById(R.id.liebiao_item_pinglun_tv);//评论数
			holder.liebiao_item_zuire_ll = (LinearLayout) view.findViewById(R.id.liebiao_item_zuire_ll);//最热评论区域
			holder.liebiao_item_zuire_text = (TextView) view.findViewById(R.id.liebiao_item_zuire_text);//最热评论内容
			
			
			//为头像添加点击事件
			holder.liebiao_item_name.setOnClickListener(userOnClickListener);
			holder.liebiao_item_icon.setOnClickListener(userOnClickListener);
			
			
			//为点赞添加点击事件
			holder.liebiao_item_ding_img.setOnClickListener(on点赞Listener);
			holder.liebiao_item_ding_tv.setOnClickListener(on点赞Listener);
			
			//为踩添加点击事件
			holder.liebiao_item_cai_img.setOnClickListener(on踩Listener);
			holder.liebiao_item_cai_tv.setOnClickListener(on踩Listener);
			
			//为转发,分享添加单击事件
			holder.liebiao_item_zhuanfa_img.setOnClickListener(on转发Listener);
			holder.liebiao_item_zhuanfa_tv.setOnClickListener(on转发Listener);
			
			//为评论添加点击事件
			holder.liebiao_item_pinglun_img.setOnClickListener(on评论Listener);
			holder.liebiao_item_pinglun_tv.setOnClickListener(on评论Listener);
			
			
			view.setTag(holder);
			
			return view;
		}

		}


		@Override
		public long getItemId(int position) {
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}
		
		@Override
		public int getCount() {
			return itemData.list.size()+1;
		}
	
	}
/*	
 * 无用代码,已被迭代
 * public 池 chi = new 池();
	public  class 池{
		 ArrayList<MyImageView> myIMGlist = new ArrayList<MyImageView>();
		public   MyImageView getImageView(int position){
			Log.e("TGAL", "IMGList--"+myIMGlist.size());
			int currentItem = pager.getCurrentItem();
			ListView listView = data.get(currentItem);
			int firstVisiblePosition = listView.getFirstVisiblePosition();
			int lastVisiblePosition = listView.getLastVisiblePosition();
			
			for (int i = 0;i<myIMGlist.size();i++) {
				MyImageView iv = myIMGlist.get(i);
				if (iv.position<firstVisiblePosition||iv.position>lastVisiblePosition) {
					iv.position = position;
					iv.初始化();
					return iv;
				}
			}
			MyImageView myImageView = new MyImageView(getContext());
			myImageView.position = position;
			myIMGlist.add(myImageView);
			return myImageView;
		}
		
		ArrayList<ShiPinView> shiPinViews = new ArrayList<ShiPinView>();
		public   ShiPinView getShiPinView(int position){
			Log.e("TGAL", "shiPinViews--"+shiPinViews.size());
			int currentItem = pager.getCurrentItem();
			ListView listView = data.get(currentItem);
			int firstVisiblePosition = listView.getFirstVisiblePosition();
			int lastVisiblePosition = listView.getLastVisiblePosition();
			
			for (int i = 0;i<shiPinViews.size();i++) {
				ShiPinView shiPin = shiPinViews.get(i);
				if (shiPin.position<firstVisiblePosition||shiPin.position>lastVisiblePosition) {
					shiPin.position = position;
					return shiPin;
				}
			}
			
			ShiPinView shiPinView = (ShiPinView) View.inflate(getContext(), R.layout.shipin_view, null);
			shiPinView.position = position;
			shiPinViews.add(shiPinView);
			return shiPinView;
		}
		
	}*/
	
	/**
	 * 当距离最下面等于5的时候,给itemData加载数据
	 * @param itemData
	 */
	protected void 加载更多(final JItemData itemData) {
		final int currentItem = pager.getCurrentItem();//当前页
		String url = 获取链接(currentItem);
		if (!"0".equals(url.substring(url.length()-1))) {
			String bendishuju = SP.getString(url);
			if (bendishuju!=null) {
				JItemData fromJson = new Gson().fromJson(bendishuju, JItemData.class);
				itemData.list.addAll(itemData.list.size()-1,fromJson.list);
				((BaseAdapter)data.get(currentItem).getAdapter()).notifyDataSetChanged();

				lastMaxtime.set(currentItem, fromJson.info.maxtime);//保存本次的记录
				return;
			}
		}
		
//		Log.e("TGAB", "url="+url);
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, url, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				//获取失败时
				Log.e("TGAB", LineInfo.getLineInfo()+"读取网络数据失败(LieBiaoView):"+arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				try {
					JItemData newItemData = new Gson().fromJson(arg0.result, JItemData.class);
					SP.putString(getRequestUrl(), arg0.result);
					lastMaxtime.set(currentItem, newItemData.info.maxtime);//保存本次的记录
					itemData.list.addAll(itemData.list.size()-1,newItemData.list);
					((BaseAdapter)data.get(currentItem).getAdapter()).notifyDataSetChanged();
				} catch (JsonSyntaxException e) {
					e.printStackTrace();
					Log.e("TGAJSON", LineInfo.getLineInfo()+","+arg0.result);
				}
			}
		});
	}

	/**
	 * 头像和名字被点击时的监听
	 * 
	 * @author FuHan
	 *
	 */
	public class userOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String userId = (String) v.getTag();
			Toast.makeText(getContext(), userId , 0).show();
			Intent intent = new Intent(getContext(), GukeActivity.class);
			intent.putExtra("uid", userId);
			getContext().startActivity(intent);
		}

	}

}
